package com.linzm.leetcode.primary.数组.二分法;

/**
 * @Author zimingl
 * @Date 2022/10/6 13:43
 * @Description: TODO
 */
public class 数组35搜索插入位置 {

    private static int intsearchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (target > nums[mid]) {
                left = mid + 1;
            } else if (target < nums[mid]) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        // or return right+1 right 多减了1
        return right+1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 3, 5, 6};
        int target = -1;
        int i = intsearchInsert(nums, target);
        System.out.println(i);
    }
}
